The calculateMPG()
method can not use the parameters of the constructor.
class Car { // instance variables double startMiles; // Stating odometer reading double endMiles; // Ending odometer reading double gallons; // Gallons of gas used between the readings // constructor Car( double first, double last, double gals ) { startMiles = first ; endMiles = last ; gallons = gals ; } // methods double calculateMPG() { return (last - first)/gals ; // WRONG, WRONG, WRONG } }
The calculateMPG()
method can not see the parameters
of the constructor.
Another way of saying this is that the scope of
the parameters is limited to the body of the method.
The compiler will complain that first, last,
and gals
are "undefined variables"
because they are used outside of their scope.
Are you about out of gas?